import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('@/views/Home.vue'),
    meta: { title: '首页', requiresAuth: false }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/auth/Login.vue'),
    meta: { title: '登录', requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/auth/Register.vue'),
    meta: { title: '注册', requiresAuth: false }
  },
  {
    path: '/community',
    name: 'Community',
    component: () => import('@/views/community/Index.vue'),
    meta: { title: '维权社区', requiresAuth: false }
  },
  {
    path: '/community/enhanced',
    name: 'CommunityEnhanced',
    component: () => import('@/views/community/Enhanced.vue'),
    meta: { title: '维权社区', requiresAuth: false }
  },
  {
    path: '/community/post/:id',
    name: 'PostDetail',
    component: () => import('@/views/community/PostDetail.vue'),
    meta: { title: '帖子详情', requiresAuth: false }
  },
  {
    path: '/community/publish',
    name: 'PublishPost',
    component: () => import('@/views/community/PublishPost.vue'),
    meta: { title: '发布帖子', requiresAuth: true }
  },
  {
    path: '/ai/diagnosis',
    name: 'AiDiagnosis',
    component: () => import('@/views/ai/Diagnosis.vue'),
    meta: { title: 'AI维权诊断', requiresAuth: true }
  },
  {
    path: '/templates',
    name: 'Templates',
    component: () => import('@/views/templates/Index.vue'),
    meta: { title: '维权模板', requiresAuth: false }
  },
  {
    path: '/templates/:id',
    name: 'TemplateDetail',
    component: () => import('@/views/templates/Detail.vue'),
    meta: { title: '模板详情', requiresAuth: false }
  },
  {
    path: '/templates/fill/:id',
    name: 'FillTemplate',
    component: () => import('@/views/templates/Fill.vue'),
    meta: { title: '填写模板', requiresAuth: true }
  },
  {
    path: '/assistance',
    name: 'Assistance',
    component: () => import('@/views/assistance/Index.vue'),
    meta: { title: '维权援助', requiresAuth: false }
  },
  {
    path: '/profile',
    name: 'Profile',
    component: () => import('@/views/profile/Index.vue'),
    meta: { title: '个人中心', requiresAuth: true }
  },
  {
    path: '/profile/info',
    name: 'ProfileInfo',
    component: () => import('@/views/profile/Info.vue'),
    meta: { title: '个人信息', requiresAuth: true }
  },
  {
    path: '/profile/collections',
    name: 'Collections',
    component: () => import('@/views/profile/Collections.vue'),
    meta: { title: '我的收藏', requiresAuth: true }
  },
  {
    path: '/profile/progress',
    name: 'Progress',
    component: () => import('@/views/profile/Progress.vue'),
    meta: { title: '维权进度', requiresAuth: true }
  },
  {
    path: '/search',
    name: 'Search',
    component: () => import('@/views/Search.vue'),
    meta: { title: '搜索', requiresAuth: false }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 维权力` : '维权力'
  
  // 检查是否需要登录
  if (to.meta.requiresAuth && !userStore.isLoggedIn) {
    next({
      path: '/login',
      query: { redirect: to.fullPath }
    })
  } else {
    next()
  }
})

export default router